package com.zou.application.controller.customer;

import java.math.BigDecimal;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.zou.application.dao.BankCarEntityMapper;
import com.zou.application.dao.UserEntityMapper;
import com.zou.application.entity.BankCarEntity;
import com.zou.application.entity.UserEntity;

@Controller
@RequestMapping("/bankcar")
public class BankCarController {
	
	@Autowired
	private UserEntityMapper userEntityMapper;
	@Autowired
	private BankCarEntityMapper bankDao;
	
	
	@RequestMapping("/bankcarPage")
	public ModelAndView contactusPage(HttpServletRequest request){
		ModelAndView mv = new ModelAndView("/customer/add_bank_car");
		return mv;
	}
	
	@RequestMapping("/bankcarInfo")
	public ModelAndView bankcarInfo(HttpServletRequest request,HttpSession session){
		ModelAndView mv = new ModelAndView("/customer/bank_car");
		Integer userId = (Integer) session.getAttribute("userId");
		try {
			BankCarEntity bankCar = bankDao.selectByUserId(userId);
			if(bankCar == null){
				mv.setViewName("/customer/add_bank_car");
			}
			mv.addObject("bankCar",bankCar);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return mv;
	}
	
	@RequestMapping("/addBankCar")
	public ModelAndView addCar(HttpServletRequest request,HttpSession session){
		ModelAndView mv = new ModelAndView();
		Integer userId = (Integer) session.getAttribute("userId");
		BankCarEntity bankCarEntity = new BankCarEntity();
		bankCarEntity.setCarType(request.getParameter("carType"));
		bankCarEntity.setCarPhone(request.getParameter("carPhone"));
		bankCarEntity.setBankAddress(request.getParameter("bankAddress"));
		bankCarEntity.setBankMoney(new BigDecimal(request.getParameter("bankMoney")));
		bankCarEntity.setBankName(request.getParameter("bankName"));
		bankCarEntity.setBankPassword(request.getParameter("bankPassword"));
		bankCarEntity.setCarNumber(request.getParameter("carNumber"));
		try {
			UserEntity user = new UserEntity();
			user = userEntityMapper.selectById(userId);
			bankCarEntity.setUserId(userId);
			bankCarEntity.setStatus((short)1);
			bankCarEntity.setCreater(user.getUserName());
			bankCarEntity.setIsDelete((short)0);
			bankCarEntity.setCreateTime(new Date());
			bankDao.insert(bankCarEntity);
			mv.addObject("bankCar",bankCarEntity);
			mv.setViewName("/customer/bank_car");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return mv;
	}
	
	@RequestMapping("/updateCarPage")
	public ModelAndView updateCarPage(HttpServletRequest request,HttpSession session){
		ModelAndView mv = new ModelAndView("/customer/add_bank_car");
		Integer userId = (Integer) session.getAttribute("userId");
		BankCarEntity bankCarEntity = new BankCarEntity();
		try {
			bankCarEntity=bankDao.selectByUserId(userId);
			mv.addObject("bankCar",bankCarEntity);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return mv;
	}
	
	@RequestMapping("/updateCar")
	public ModelAndView updateCar(HttpServletRequest request,HttpSession session){
		ModelAndView mv = new ModelAndView();
		Integer userId = (Integer) session.getAttribute("userId");
		BankCarEntity bankCarEntity = new BankCarEntity();
		bankCarEntity.setCarType(request.getParameter("carType"));
		bankCarEntity.setCarPhone(request.getParameter("carPhone"));
		bankCarEntity.setBankAddress(request.getParameter("bankAddress"));
		bankCarEntity.setBankMoney(new BigDecimal(request.getParameter("bankMoney")));
		bankCarEntity.setBankName(request.getParameter("bankName"));
		bankCarEntity.setBankPassword(request.getParameter("bankPassword"));
		bankCarEntity.setCarNumber(request.getParameter("carNumber"));
		try {
			UserEntity user = new UserEntity();
			user = userEntityMapper.selectById(userId);
			bankCarEntity.setModifier(user.getUserName());
			bankCarEntity.setModifyTime(new Date());
			bankDao.update(bankCarEntity);
			mv.addObject("bankCar",bankCarEntity);
			mv.setViewName("/customer/bank_car");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return mv;
	}
}
